package main

import (
	"fmt"
	"sync"
)

//1. 读锁不能阻塞读锁
//2. 读锁需要阻塞写锁，直到所有读锁都释放
//3. 写锁需要阻塞读锁，直到所有写锁都释放
//4. 写锁需要阻塞写锁

//读写互斥锁，写的时候加索单个执行，读取则并发执行
var rwlock sync.RWMutex
var wg sync.WaitGroup

func onlyWrite() {
	rwlock.Lock()  //写锁
	fmt.Println("写操作")
	rwlock.Unlock()
	wg.Done()
}

func onlyRead() {
	rwlock.RLock()  //读锁
	fmt.Println(">>>>读操作")
	rwlock.RUnlock()
	wg.Done()
}

func main() {
	for i := 0; i < 20; i++ {
		wg.Add(1)
		go onlyWrite()
	}

	for i := 0; i < 20; i++ {
		wg.Add(1)
		go onlyRead()
	}
	wg.Wait()
}
